package com.dongdongshop.service.impl;

import com.dongdongshop.data.PageBean;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.SpecTwo;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public PageBean<TbSpecification> findList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(null);
        PageInfo<TbSpecification> pageInfo = new PageInfo<>(tbSpecifications);
        return new PageBean<>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getList());
    }

    @Override
    public void deletefication(Long[] id) {
        tbSpecificationMapper.deletefication(id);
        tbSpecificationOptionMapper.deletefication(id);
    }

    @Override
    public void addInfo(List<TbSpecificationOption> options, TbSpecification tbSpecification) {
        tbSpecificationMapper.addInfo(tbSpecification);
        tbSpecificationOptionMapper.addInfo(tbSpecification.getId(),options);
    }

    @Override
    public SpecTwo updateById(Long id) {
        SpecTwo specTwo = new SpecTwo();
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        List<TbSpecificationOption> optionList = tbSpecificationOptionMapper.selectById(id);
        specTwo.setTbSpecification(tbSpecification);
        specTwo.setOptionList(optionList);
        return specTwo;
    }

    @Override
    public void updateSpec(List<TbSpecificationOption> options1, TbSpecification tbSpecification) {
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        tbSpecificationOptionMapper.deleteSpecInfo(tbSpecification.getId());
        tbSpecificationOptionMapper.addInfo(tbSpecification.getId(), options1);
    }

    @Override
    public SpecTwo getOneSpecInfo(Long id) {
        SpecTwo specTwo = new SpecTwo();
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        List<TbSpecificationOption> tbSpecificationOptionList = tbSpecificationOptionMapper.getInfo(id);
        specTwo.setTbSpecification(tbSpecification);
        specTwo.setOptionList(tbSpecificationOptionList);
        return specTwo;
    }

}
